const Koa = require('koa');
//引入跨域包;
const cors = require('koa2-cors');
const koaBody = require('koa-body');
const koaStatic = require('koa-static');
const error = require('koa-json-error');
const parameter = require('koa-parameter');
const mongoose = require('mongoose');
const path = require('path');
const app = new Koa();
const routing = require('./routers');
const {connetctionStr} = require('./config');

mongoose.connect(connetctionStr,{useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify : false},()=>{
    console.log('MongoDB链接成功...');
});
mongoose.connection.on('error',console.error);

app.use(cors());
app.use(koaStatic(path.join(__dirname,'/public')));
app.use(error({
    postFormat: (e,{stack, ...rest}) => process.env.NODE_ENV === 'production' ? rest : {stack,...rest}
}));
app.use(koaBody({
    multipart :true,
    formidable: {
        uploadDir:path.join(__dirname,'/public/uploads'),
        keepExtensions:true
    }
}))

app.use(parameter(app));
routing(app);

app.listen(8081,()=>{
    console.log('服务器启动成功!');
})